package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.anno.GuiGuCache;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	private final BaseCategory1Mapper baseCategory1Mapper;

	private final BaseCategory2Mapper baseCategory2Mapper;

	private final BaseCategory3Mapper baseCategory3Mapper;

	private final BaseCategoryViewMapper baseCategoryViewMapper;

	private final BaseAttributeMapper baseAttributeMapper;

	@Autowired
	public BaseCategoryServiceImpl(BaseCategory1Mapper baseCategory1Mapper, BaseCategory2Mapper baseCategory2Mapper, BaseCategory3Mapper baseCategory3Mapper, BaseCategoryViewMapper baseCategoryViewMapper, BaseAttributeMapper baseAttributeMapper) {
		this.baseCategory1Mapper = baseCategory1Mapper;
		this.baseCategory2Mapper = baseCategory2Mapper;
		this.baseCategory3Mapper = baseCategory3Mapper;
		this.baseCategoryViewMapper = baseCategoryViewMapper;
		this.baseAttributeMapper = baseAttributeMapper;
	}

	/**
	 * @description: 查询所有专辑分类数据
	 * @author Petrichor
	 * @date 2023/12/20 10:12
	 * @return AlbumCategoryVo 专辑分类数据 [专辑分类名称,专辑分类Id,专辑子分类集合]
	 **/
	@Override
	public List<AlbumCategoryVo> getBaseCategoryList() {

		return baseCategoryViewMapper.getBaseCategoryList();
	}
	/**
	 * @description: 查询专辑属性:根据分类Id查询专辑属性
	 * @author Petrichor
	 * @date 2023/12/21 18:33
	 * @param id 一级分类id
	 * @return 专辑属性
	 **/
	@Override
	public List<BaseAttribute> findAttribute(Long id) {
		List<BaseAttribute> baseAttributeList = baseAttributeMapper.findAttribute(id);
		return baseAttributeList;
	}
	/**
	 * @description: 根据三级分类id查询分类数据
	 * @author Petrichor
	 * @date 2023/12/26 20:46
	 * @param c3Id Category3Id 专辑三级分类id
	 * @return BaseCategoryView 分类视图 一二三级分类的id和名称
	 **/
	@GuiGuCache(
			cacheKey = "category:info:" + "#{#params[0]}" ,
			enableLock = true ,
			lockName = "category:lock:" + "#{#params[0]}"
	)
	@Override
	public BaseCategoryView findCategoryByC3Id(Long c3Id) {
		BaseCategoryView baseCategoryView = baseCategory3Mapper.findCategoryByC3Id(c3Id);
		return baseCategoryView;
	}
	/**
	 * @description: 根据一级分类id获取全部分类信息
	 * @author Petrichor
	 * @date 2023/12/27 22:31
	 * @param category1Id 一级分类id
	 * @return AlbumCategoryVo 专辑分类数据
	 **/
	@Override
	public AlbumCategoryVo getAllCategoryList(Long category1Id) {
		return baseCategoryViewMapper.getCategoryInfoByCategory1Id(category1Id);
	}
	/**
	 * @description: 根据一级分类id查询置顶三级分类
	 * @author Petrichor
	 * @date 2023/12/27 22:39
	 * @param category1Id 一级分类id
	 * @return 三级分类集合
	 **/
	@Override
	public List<BaseCategory3> findTopBaseCategory3ByCategory1Id(Long category1Id) {

		//	先根据一级分类Id 找到二级分类集合
		LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id,category1Id);
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);
		List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());

		//	查询置顶消息
		LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();

		// last("limit 7") 查询前7条
		wrapper.in(BaseCategory3::getCategory2Id,category2IdList).eq(BaseCategory3::getIsTop,1).last("limit 7");

		// 查询数据返回结果
		return baseCategory3Mapper.selectList(wrapper);

	}
}
